package com.seeyon.apps.quartz.util;

import com.seeyon.ctp.common.quartz.QuartzHolder;

import java.util.Date;
import java.util.Map;

/**
 * 定时任务注册及删除
 * @author fgw
 */
public class QsQuartzUtil {
	
	/**
	 * 注册
	 */
	public static boolean registerSyncTask(String jobBeanId, String jobName, String cron, Date begin, Date end, Map<String, String> params) throws Exception {
		// 先删除任务
		QuartzHolder.deleteQuartzJobByGroupAndJobName("CustomQuartz", jobName);
		if(null == begin) {
			begin = new Date();
		}
		return QuartzHolder.newCronQuartzJob("CustomQuartz", jobName, cron, begin, end, jobBeanId, params);
    }
	
	/**
	 * 删除
	 * @param jobName
	 * @throws Exception
	 */
	public static void deleteJob(String jobName) throws Exception {
		boolean hasJob = QuartzHolder.hasQuartzJob("CustomQuartz", jobName);
		if(!hasJob) {
			throw new Exception("定时任务名称不存在：" + jobName);
		}
		QuartzHolder.deleteQuartzJobByGroupAndJobName("CustomQuartz", jobName);
	}

	public static void deleteJob(String groupName, String jobName) throws Exception {
		boolean hasJob = QuartzHolder.hasQuartzJob(groupName, jobName);
		if(!hasJob) {
			throw new Exception("定时任务名称不存在：" + jobName);
		}
		QuartzHolder.deleteQuartzJobByGroupAndJobName(groupName, jobName);
	}
}
